<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;


class ArticleController extends Controller
{

    /**
     * @OA\Post(
     *     path="/admin/article_index",
     *     tags={"文章"},
     *     summary="列表",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"state"},
     *                 @OA\Property(property="key", type="string", description="搜索字段   标题 title,category"),
     *                 @OA\Property(property="val", type="string", description="搜索值(模糊搜索)"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function index(Request $request)
    {
        $where = [];
        $post_data = $request->all();

        if($request->has('limit')){
            $limit = $request->input('limit');
        }else{
            $limit = 15;
        }

        if(isset($post_data['key']) && isset($post_data['val']) && $post_data['key'] != "" && $post_data['val'] != ""){
        $where[] = [$post_data['key'],'like','%'.$post_data['val'].'%'];
    }

        $admin_list = Db::table('article')
            ->where($where)
            ->select('title','id','add_time','pic','synopsis','category')
            ->orderByDesc('id')
            ->paginate($limit);
        return s_msg('',$admin_list);
    }

    /**
     * @OA\Post(
     *     path="/admin/article_xq",
     *     tags={"文章"},
     *     summary="详情",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"article_id"},
     *                 @OA\Property(property="article_id", type="number", description="文章id"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function xq(Request $request)
    {
        if($request->has('article_id')){
            $admin_info = Db::table('article')
                ->where('id',$request->input('article_id'))
                ->first();
            if($admin_info){
                return s_msg('',$admin_info);
            }else{
                return e_msg(300020,'文章信息不存在');
            }

        }else{
            return e_msg(300019,'未传入文章id article_id');
        }

    }

    /**
     * @OA\Post(
     *     path="/admin/article_delete",
     *     tags={"文章"},
     *     summary="删除",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"article_id"},
     *                 @OA\Property(property="article_id", type="number", description="文章id"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function delete(Request $request)
    {
        if($request->has('article_id')){

            $admin_info = Db::table('article')
                ->where('id',$request->input('article_id'))
                ->first();
            if($admin_info){
                $rel = DB::table('article')->where('id',$request->input('article_id'))->delete();
                return s_msg('');
            }else{
                return e_msg(300020,'文章信息不存在');
            }

        }else{
            return e_msg(300019,'未传入id');
        }
    }

    /**
     * @OA\Post(
     *     path="/admin/article_add",
     *     tags={"文章"},
     *     summary="添加",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"title","content","pic"},
     *                 @OA\Property(property="title", type="string", description="标题"),
     *                 @OA\Property(property="content", type="string", description="内容"),
     *                 @OA\Property(property="pic", type="string", description="图片"),
     *                 @OA\Property(property="synopsis", type="string", description="简介"),
     *                 @OA\Property(property="category", type="string", description="分类"),
     *                 @OA\Property(property="start_time", type="string", description="只有公告推送添加这个"),
     *                 @OA\Property(property="end_time", type="string", description="只有公告推送添加这个"),
     *                 @OA\Property(property="color", type="string", description="颜色"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function add(Request $request)
    {
        $post_data = $request->all();

        $validateRule = [
            'title'                      => 'required',
            'content'                      => 'required',
            'pic'                      => 'required',
            'synopsis'                      => 'required',
            'category'                      => 'required',
            'color'                      => 'required',
        ];
        $errorMsg = [
            'title.required'                 => '文章标题不能为空',
            'content.required'               => '文章内容不能为空',
            'pic.required'                 => '文章主图不能为空',
            'synopsis.required'                 => '文章简介不能为空',
            'category.required'                 => '分类不能为空',
            'color.required'                 => 'color不能为空',
        ];

        $validator = Validator::make($post_data, $validateRule, $errorMsg);
        if ($validator->fails()){
            $errors = $validator->errors()->all();
            return e_msg(300001,$errors[0]);
        }

        $insert_arr = [
            'title' => $post_data['title'],
            'content' => $post_data['content'],
            'pic' => $post_data['pic'],
            'synopsis' => $post_data['synopsis'],
            'category' => $post_data['category'],
            'color' => $post_data['color'],

        ];

        if($post_data['category'] == '公告推送'){

            $insert_arr['start_time'] = $post_data['start_time'];
            $insert_arr['end_time'] = $post_data['end_time'];

        }


        $rel= DB::table('article')->insert($insert_arr);
        if($rel){
            return s_msg('');
        }else{
            return e_msg(300006,'请联系后台');
        }

    }

    /**
     * @OA\Post(
     *     path="/admin/article_edit",
     *     tags={"文章"},
     *     summary="编辑",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                  required={"title","content","pic","id"},
     *                 @OA\Property(property="id", type="string", description="ID"),
     *                 @OA\Property(property="title", type="string", description="标题"),
     *                 @OA\Property(property="content", type="string", description="内容"),
     *                 @OA\Property(property="pic", type="string", description="图片"),
     *                 @OA\Property(property="synopsis", type="string", description="简介"),
     *                 @OA\Property(property="category", type="string", description="分类"),
     *                 @OA\Property(property="start_time", type="string", description="只有公告推送添加这个"),
     *                 @OA\Property(property="end_time", type="string", description="只有公告推送添加这个"),
     *                 @OA\Property(property="color", type="string", description="color"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function edit(Request $request)
    {
        $post_data = $request->all();

        $validateRule = [
            'title'                      => 'required',
            'content'                      => 'required',
            'pic'                      => 'required',
            'synopsis'                      => 'required',
            'category'                      => 'required',
            'id'                      => 'required',
            'color'                     =>'required',
        ];
        $errorMsg = [
            'title.required'                 => '文章标题不能为空',
            'content.required'                 => '文章内容不能为空',
            'pic.required'                 => '文章主图不能为空',
            'synopsis.required'                 => '文章简介不能为空',
            'category.required'                 => '分类不能为空',
            'id.required'                 => '请传入id',
            'color.required'                 => '请传入color',

        ];

        $validator = Validator::make($post_data, $validateRule, $errorMsg);
        if ($validator->fails()){
            $errors = $validator->errors()->all();
            return e_msg(300001,$errors[0]);
        }

        $update_arr = [
            'title' => $post_data['title'],
            'content' => $post_data['content'],
            'pic' => $post_data['pic'],
            'category' => $post_data['category'],
            'synopsis' => $post_data['synopsis'],
            'color' => $post_data['color'],
        ];


        if($post_data['category'] == '公告推送'){

            $update_arr['start_time'] = $post_data['start_time'];
            $update_arr['end_time'] = $post_data['end_time'];

        }


        $rel= DB::table('article')->where('id',$post_data['id'])->update($update_arr);
        if($rel){
            return s_msg('');
        }else{
            return e_msg(300006,'您未作出改变');
        }

    }


    /**
     * @OA\Post(
     *     path="/admin/pic",
     *     tags={"文章"},
     *     summary="主图上传",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                  required={"pic"},
     *                 @OA\Property(property="pic", type="string", description="文件对象"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function pic(Request $request)
    {
        if ($request->hasFile('pic')) {

            if ($request->file('pic')->isValid()) {

                $image = $request->file('pic');
                $path=date('Ymd');
                $rel = Storage::put($path,$image);
                $url = get_http_type().$_SERVER['HTTP_HOST'].'/uploads/orders/'.$rel;
                return s_msg('',$url);
            }else{
                return e_msg('请上传有效文件');
            }

        }else{
            return e_msg('您上传的图片为空');
        }

    }
}
